const app = getApp()
Page({
  data: {
    userInfo: {},
    checkedUserInfo: {},
    formData: {
      isContact: '0',
      isArrive: '0',
      access: '0'
    },
    error: '',
    isContactItems: [{
        name: '否',
        value: '0',
        checked: true
      },
      {
        name: '是',
        value: '1'
      }],
    isArriveItems: [{
      name: '否',
      value: '0',
      checked: true
    },
    {
      name: '是',
      value: '1'
    }],
    accessItems: [{
      name: '出',
      value: '0',
      checked: true
    },
    {
      name: '进',
      value: '1'
    }],
    rules: [{
      name: 'temperature',
      rules: {
        required: true,
        message: '体温必填'
      },
    }]
  },

  onLoad: function (options) {
    this.setData({
      userInfo: wx.getStorageSync('userInfo'),
      checkedUserInfo: wx.getStorageSync('checkedUserInfo'),
    })
  },
  isContactChange: function (e) {
    var isContactItems = this.data.isContactItems;
    for (var i = 0, len = isContactItems.length; i < len; ++i) {
      isContactItems[i].checked = isContactItems[i].value == e.detail.value;
    }

    this.setData({
      isContactItems: isContactItems,
      [`formData.isContact`]: e.detail.value
    });
  },
  isArriveChange: function (e) {
    var isArriveItems = this.data.isArriveItems;
    for (var i = 0, len = isArriveItems.length; i < len; ++i) {
      isArriveItems[i].checked = isArriveItems[i].value == e.detail.value;
    }

    this.setData({
      isArriveItems: isArriveItems,
      [`formData.isArrive`]: e.detail.value
    });
  },
  accessChange: function (e) {
    var accessItems = this.data.accessItems;
    for (var i = 0, len = accessItems.length; i < len; ++i) {
      accessItems[i].checked = accessItems[i].value == e.detail.value;
    }

    this.setData({
      accessItems: accessItems,
      [`formData.access`]: e.detail.value
    });
  },
  formInputChange(e) {
    const { field } = e.currentTarget.dataset
    this.setData({
      [`formData.${field}`]: e.detail.value
    })
  },
  submitForm() {
    this.selectComponent('#form').validate((valid, errors) => {
      //是否有错误
      if (!valid) {
        const firstError = Object.keys(errors)
        if (firstError.length) {
          this.setData({
            error: errors[firstError[0]].message,
          })
        } 
      } else {
        const that = this
        wx.request({
          url: app.globalData.serverUrl + '/health',
          method: 'post',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          data: {
            openid: that.data.checkedUserInfo.openid,
            temperature: that.data.formData.temperature,
            isContact: parseInt(that.data.formData.isContact),
            isArrive: parseInt(that.data.formData.isArrive),
            access: parseInt(that.data.formData.access),
            skey: that.data.userInfo.skey
          },
          success: function (res) {
            if (res.data.status == 200) {
              wx.showToast({
                title: '提交成功',
                icon: 'none',
                duration: 1500
              })
              wx.redirectTo({
                url: '/pages/home-checker/home-checker',
              })
            } else {
              wx.showToast({
                title: '提交失败',
                icon: 'none',
                duration: 1500
              })
            }
          },
          fail: function (error) {
            console.log(error)
            //调用服务端登录接口失败
            wx.showToast({
              title: '请确保网络通畅',
              icon: 'none',
              duration: 2000
            })
          }
        })

      }
    })

  }

  

}) 